import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login/index.vue'),
      hidden: true
    },
    {
      path: '/',
      component: Layout,
      redirect: '/dashboard',
      meta: { title: '工作台', icon: 'Menu' },
      children: [
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('@/views/dashboard/index.vue'),
          meta: { title: '首页' }
        }
      ]
    },
    {
      path: '/user',
      component: Layout,
      meta: { title: '用户管理', icon: 'User' },
      children: [
        {
          path: 'list',
          name: 'UserList',
          component: () => import('@/views/user/List.vue'),
          meta: { title: '用户列表' }
        }
      ]
    },
    {
      path: '/staff',
      component: Layout,
      meta: { title: '员工管理', icon: 'User' },
      children: [
        {
          path: 'list',
          name: 'StaffList',
          component: () => import('@/views/staff/List.vue'),
          meta: { title: '员工列表' }
        }
      ]
    },
    {
      path: '/restaurant',
      component: Layout,
      meta: { title: '餐厅管理', icon: 'Shop' },
      children: [
        {
          path: 'list',
          name: 'RestaurantList',
          component: () => import('@/views/restaurant/List.vue'),
          meta: { title: '餐厅列表' }
        },
        {
          path: 'table',
          name: 'TableList',
          component: () => import('@/views/reservation/Table.vue'),
          meta: { title: '餐桌管理' }
        },
      ]
    },
    {
      path: '/dish',
      component: Layout,
      meta: { title: '菜品管理', icon: 'Food' },
      children: [
        {
          path: 'category',
          name: 'DishCategory',
          component: () => import('@/views/dish/Category.vue'),
          meta: { title: '菜品分类' }
        },
        {
          path: 'list',
          name: 'DishList',
          component: () => import('@/views/dish/List.vue'),
          meta: { title: '菜品列表' }
        }
      ]
    },
    {
      path: '/order',
      component: Layout,
      meta: { title: '订单管理', icon: 'Calendar' },
      children: [
        {
          path: 'list',
          name: 'OrderList',
          component: () => import('@/views/order/List.vue'),
          meta: { title: '预订列表' }
        }
      ]
    },

  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const token = localStorage.getItem('token')
  const expireTime = localStorage.getItem('expireTime')

  // 检查token是否过期
  const checkTokenExpired = () => {
    if (!expireTime) return true
    return new Date().getTime() > parseInt(expireTime)
  }

  if (to.path === '/login') {
    if (token && !checkTokenExpired()) {
      next('/')
    } else {
      next()
    }
  } else {
    if (!token || checkTokenExpired()) {
      if (token) {
        ElMessage.warning('登录已过期，请重新登录')
        userStore.logout()
      }
      next('/login')
    } else {
      next()
    }
  }
})

export default router
